import {
  createMemoryHistory,
  createRouter,
  createWebHashHistory
} from 'vue-router'

import HomeView from '../views/Home/index.vue'
import Show from '../views/showing/index.vue'
import Login from "../views/Login/index.vue";
import Register from "../views/Register/Register.vue";
import Active from "../views/active/index.vue";
import SelectActive from "../views/SelectActive/SelectActive.vue";
import Test from "../views/test/index.vue";
import Search from "../views/Search/Search.vue"
import Composition from "../views/composition/composition.vue"
import NewWord from "../views/NewWord/NewWord.vue"
import Review from "../views/review/review.vue"
import Forum from "../views/Forum/Forum.vue"
import LearningResources from "../views/LearningResources/LearningResources.vue"
import MemorizationSkills from "../views/MemorizationSkills/MemorizationSkills.vue";
import Mine from "../views/mine/mine.vue";
import Post from "../views/post/post.vue";
import SystemNotifications from "../views/SystemNotifications/SystemNotifications.vue"
import Dashboard from "../views/Dashboard/Dashboard.vue";
import User from "../views/User/User.vue";
import Categories from "../views/Categories/Categories.vue";
import Words from "../views/Words/Words.vue";
import LogViewer from "../views/LogViewer/LogViewer.vue";
import LogViewerStudy from "../views/LogViewerStudy/LogViewerStudy.vue";
import ArticleAdminPage from "../views/ArticleAdminPage/ArticleAdminPage.vue";
import Sentences from '../views/sentences/sentences.vue';
import SynonymsAntonyms from "../views/SynonymsAntonyms/SynonymsAntonyms.vue";


const routes = [
  {
    path: '/Home',
    component: HomeView,  // HomeView 作为父路由
    meta: {
      requiresAuth: true
    },
    children: [
      {
        path: 'User',  // 子路由路径为 `/Home/User`
        component: User,
        meta: {
          requiresAuth: true
        }
      },
      {
        path: 'Dashboard',  // 子路由路径为 `/Home/Dashboard`
        component: Dashboard,
        meta: {
          requiresAuth: true
        }
      },
      {
        path: 'Categories',  // 子路由路径为 `/Home/Dashboard`
        component: Categories,
        meta: {
          requiresAuth: true
        }
      },
      {
        path: 'Words',  // 子路由路径为 `/Home/Dashboard`
        component: Words,
        meta: {
          requiresAuth: true
        }
      },
      {
        path: 'LogViewer',  // 子路由路径为 `/Home/Dashboard`
        component: LogViewer,
        meta: {
          requiresAuth: true
        }
      },
      {
        path: 'LogViewerStudy',  // 子路由路径为 `/Home/Dashboard`
        component: LogViewerStudy,
        meta: {
          requiresAuth: true
        }
      },
      {
        path: 'ArticleAdminPage',  // 子路由路径为 `/Home/Dashboard`
        component: ArticleAdminPage,
        meta: {
          requiresAuth: true
        }
      },
      {
        path: 'Sentences',  // 子路由路径为 `/Home/Dashboard`
        component: Sentences,
        meta: {
          requiresAuth: true
        }
      },
      {
        path: 'SynonymsAntonyms',  // 子路由路径为 `/Home/Dashboard`
        component: SynonymsAntonyms,
        meta: {
          requiresAuth: true
        }
      }
    ]
  },
  {
    path: '/',
    component: Show
  }, // 公开页面
  {
    path: '/Login',
    component: Login
  },
  {
    path: '/Register',
    component: Register
  },
  {
    path: '/active',
    component: Active,
    meta: {
      requiresAuth: true
    }
  }, // 需要登录
  {
    path: '/SelectActive',
    component: SelectActive,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/Test',
    component: Test,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/Search',
    component: Search,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/Composition',
    component: Composition,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/NewWord',
    component: NewWord,
    meta: {
      requiresAuth: true
    }
  }
  ,
  {
    path: '/Review',
    component: Review,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/Forum',
    component: Forum,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/LearningResources',
    component: LearningResources,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/MemorizationSkills',
    component: MemorizationSkills,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/Mine',
    component: Mine,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/Post/:id',
    component: Post,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/SystemNotifications',
    component: SystemNotifications,
    meta: {
      requiresAuth: true
    }
  }
];


const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

// 全局路由守卫，判断是否登录
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token'); // 获取 token
  console.log(isAuthenticated, 'isAuthenticated')

  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/Login'); // 未登录，跳转到登录页
  } else {
    next(); // 继续导航
  }
});




export default router